const express = require('express')

const router = require('./16-apiRouter')

const cors = require('cors')

const app = express()

// jsonp一定要定义在cors前，否则会配置成cors
app.get('/api/jsonp', (req, res) => {
  const func = req.query.callback
  console.log(func)
  // 定义返回的数据
  const data = {
    name: 'zs',
    age: 18
  }
  const str = `${func}(${JSON.stringify(data)})` // 将对象转换为字符串
  res.send(str)
})

// 配置cors, 允许跨域
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: false }))

// 挂载路由
app.use('/api', router)

app.listen(80, () => {
  console.log('express server running at http://127.0.0.1')
})